using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Chapter10CustomControlsCS
{
    [ToolboxData("<{0}:Consumer runat=server></{0}:Consumer>")]
    public class ConsumerPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        private IBookInfo ibk;
        private System.Web.UI.WebControls.WebParts.IWebPartField ifld;
        private string strTitle = "Custom Controls and Web Parts";
        private string strAuthor = "Peter Vogel";

        public string BookTitle
        {
            get
            {
                return strTitle;
            }
            set
            {
                strTitle = value;
            }

        }

        public string BookAuthor
        {
            get
            {
                return strAuthor;
            }
            set
            {
                strAuthor = value;
            }

        }

        [System.Web.UI.WebControls.WebParts.ConnectionConsumer("IBookInfo Consumer","Book")]
        public void IBookInfoConsumer(IBookInfo bk)
        {
            string strISBN;
            string strProviderValue;
            bool bolSuccess;

            strISBN = bk.ISBN;
            ibk = bk;

            strProviderValue = "10345329";
            bk.ISBN = strProviderValue;
            bk.UpdateCache(true);
            bolSuccess = bk.SortData("Desc");
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (ibk == null)
            {
                writer.Write("<b>No book information available.</b>");
            }
            else
            {
                writer.Write("ISBN: <b>" + ibk.ISBN + "</b>");
            }
        }


        [System.Web.UI.WebControls.WebParts.ConnectionConsumer("IWebPartField Consumer", "Field")]
        public void IWebPartFieldConsumer(System.Web.UI.WebControls.WebParts.IWebPartField fld)
        {
            ifld = fld;
            fld.GetFieldValue(new System.Web.UI.WebControls.WebParts.FieldCallback(RecieveDataField));
        }

        public void RecieveDataField(object BookTitle)
        {
            PropertyDescriptor pd;

            pd = ifld.Schema;
            if (pd.PropertyType.Name == "String")
            {
                strTitle = BookTitle.ToString();
            }
        }
        

        [System.Web.UI.WebControls.WebParts.ConnectionConsumer("IParameters Consumer")]
        public void IParametersConsumer(System.Web.UI.WebControls.WebParts.IWebPartParameters prm)
        {
            PropertyDescriptorCollection pdc;
            System.Web.UI.WebControls.WebParts.IWebPartParameters iprm;

            iprm = prm;

            pdc = TypeDescriptor.GetProperties(this);
            iprm.SetConsumerSchema(pdc);
            if (pdc.Count == iprm.Schema.Count)
            {
                iprm.GetParametersData(ReceiveDataParm);
            }
        }

        public void ReceiveDataParm(System.Collections.IDictionary BookInfo)
        {
            foreach (System.Collections.DictionaryEntry entry in BookInfo.Keys)
            {
                switch (entry.Key.ToString())
                {
                    case "BookTitle":
                        this.BookTitle = entry.Value.ToString();
                        break;

                    case "BookAuthor":
                        this.BookAuthor = entry.Value.ToString();
                        break;
                };
            }
        }
    }
}
